package com.example.administrator.maizuofilm.cinema.fragment;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;


import com.alibaba.fastjson.JSON;
import com.example.administrator.maizuofilm.BaseFragment;
import com.example.administrator.maizuofilm.R;
import com.example.administrator.maizuofilm.bean.Cinema;
import com.example.administrator.maizuofilm.cinema.adapter.MyExpandAdapter;
import com.example.administrator.maizuofilm.cinema.view.CustomExpandableListView;
import com.example.administrator.maizuofilm.cinema.view.UrlUtils;
import com.example.administrator.maizuofilm.cinemaschedule.CinemaSchedulingActivity;
import com.example.administrator.maizuofilm.cinemaschedule.utils.MyApplication;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;

public class CinemaFragment extends BaseFragment {
   public  static  final  String TAG=CinemaFragment.class.getSimpleName();
	private CustomExpandableListView expandList;
	private   HttpUtils  httpUtils;
	private  List<String>  parentRepeat;
	private  List<String>  parent;
	private  List<List<Cinema>> child;
	private MyExpandAdapter adapter;
	private  ProgressDialog  pd;
//	private MyApplication app;
	private   String cityId;
	private  String  path;
   @Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
	     View view=inflater.inflate(R.layout.fragment_cinema, null);
//	     app=(MyApplication) getActivity().getApplication();
//		  cityId=app.getCityId();
		  cityId="12";
		  path= UrlUtils.cinemaList+cityId+UrlUtils.cinemaLast;
		  Log.i("tag", "path+++++++"+path);
		  httpUtils=new HttpUtils();
		  initView(view);
		  initData();
		  event();
		return view;
	}
   public  void  initView(View view){
	   expandList=(CustomExpandableListView) view.findViewById(R.id.expandlist);
   }
   public  void  initData(){
	  
	   pd=new ProgressDialog(getActivity());
	   pd.setMessage("加载中...");
	   pd.show();
	     httpUtils.send(HttpMethod.GET,path, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				throw new RuntimeException("网络连接失败");
			}

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				String  jsonString=responseInfo.result;
				
				 try {
					List<Cinema> list=JSON.parseArray(new JSONObject(jsonString)
					              .getString("cinemaList").toString(), Cinema.class);
					parentRepeat=new ArrayList<String>();
			         for(Cinema cm:list){
			        	 parentRepeat.add(cm.getRegionName());
			         }
				      parent=wipeRepeat(parentRepeat);
				      Log.i("tag", "城区的数目++++++++++"+parent.size());
				      child=new ArrayList<List<Cinema>>();
				      for(int i=0;i<parent.size();i++){
				    	  List<Cinema> list2=new ArrayList<Cinema>();
				    	  for(int j=0;j<list.size();j++){
				    		 String reginName=list.get(j).getRegionName();
				    		  if(parent.get(i).equals(reginName)){
				    			  list2.add(list.get(j));
				    		  }
				    	  }
				    	  child.add(list2);
				      }
				      Log.i("tag", "城区影院的数目++++++++++"+child.size());
				      adapter=new MyExpandAdapter(getActivity(), parent, child);
				      expandList.setAdapter(adapter);
				      pd.dismiss();
				      
				 } catch (JSONException e) {
					e.printStackTrace();
				}
			}
		});
   }
   public  void  event(){
	   expandList.setOnChildClickListener(new OnChildClickListener() {
		
		@Override
		public boolean onChildClick(ExpandableListView parent, View v,
				int groupPosition, int childPosition, long id) {
			CinemaSchedulingActivity.cinemaName=child.get(groupPosition).get(childPosition).getCinemaName();
			CinemaSchedulingActivity.cinemaId=child.get(groupPosition).get(childPosition).getCinemaID();
			startActivity(new Intent(getActivity(),CinemaSchedulingActivity.class));
			return false;  
		}
	});
   }
  public  List<String>   wipeRepeat(List<String> list){
	  List<String>  newlist=new ArrayList<String>();
	  HashSet<String> hs=new HashSet<String>(list);
	  Iterator<String> it=hs.iterator();
	  while(it.hasNext()){
		  newlist.add(it.next());
	  }
	  return newlist;
  }
}
